<template>
	<view>
		<view class="list b-b" v-if="addressData.name">
			<view class="left">
				<view>收货地址</view>
			</view>
			<view class="middle">
				<view class="address-box">
					<text class="merchant">{{addressData.name}}   {{addressData.telephone}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{addressData.province}}{{addressData.city}}{{addressData.area}}{{addressData.street}}</text>
				</view>
			</view>
			<view class="action">
				<view class="button-base-outline" @click="changeAddress()">切换</view>
			</view>
		</view>
		<view class="list b-b" v-else>
			<icon type="warn" size="20"/>
			<view class="no-merchant">
				你还没有选择收货地址
			</view>
			<view class="action">
				<view class="button-base-outline" @click="changeAddress()">马上选择</view>
			</view>
		</view>
		<view class="goods-section">
			<!-- 商品列表 -->
			<view>
				<view class="g-item">
					<image :src="product.productMainImage.url"></image>
					<view class="right">
						<text class="title clamp">{{product.productName}}</text>
						<text class="spec" v-if="product.skuEnabled">
							<text v-for="sku in productSku.skuAttrValueList">{{sku.skuAttrValue}}</text>
						</text>
						<text class="spec" v-if="!product.skuEnabled">-</text>
						<view class="price-box">
							<text class="price" v-if="product.skuEnabled">{{productSku.skuUnitPoint}}</text>
							<text class="price" v-if="!product.skuEnabled">￥{{product.unitPoint}}</text>
							<text class="number">x {{unit}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品积分</text>
				<text class="cell-tip red">{{productPoint}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">￥{{freightAmount}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="memo" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{actualAmount}}</text>
			</view>
			<view class="submit">
				<view class="button-base" @click="submit">
					提交订单
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	export default {
		data() {
			return {
				addressData: {}, //收货地址
				product:{
					productMainImage:{
						
					}
				},
				productSku:{},
				productId:'',
				productSkuId:'',
				groupBuyId:'',
				unit:1,
				productPoint: 0, //商品积分
				productAmount: 0.00, //商品金额, 未扣减任何优惠
				deductAmount: 0.00, //优惠金额
				actualAmount: 0.00, //实付金额
				freightAmount: 0.00, //运费
				memo: '', //备注
				payType: 1, //1微信 2支付宝
				type: ''
			}
		},
		components: {
			uniPopup
		},
		onLoad(option) {
			//商品数据
			this.productId = option.productId;
			this.productSkuId = option.productSkuId;
			this.groupBuyId = option.groupBuyId;
			this.unit = option.unit;
			this.inquiryPointProductById(this.productId);
			this.inquiryDefaultAddress(this.userInfo.userUuid);
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'hasMerchant', 'merchantInfo', 'applicationConfig'])
		},
		watch: {
			addressData(val) {
				this.calculatePointFreightAmount(val.province, val.city, val.area);
			}
		},
		methods: {
			changeAddress(item){
				uni.navigateTo({
					url: '/pages/address/address?source=1'
				})
			},
			//查询默认收货地址
			inquiryDefaultAddress(userUuid) {
				let that = this;
				let postData = {
					userDTO: {
						userUuid: userUuid
					}
				}
				this.$api.request.userShip(postData, res => {
					if (res.body.status.statusCode === '0') {
						res.body.data.addresses.forEach(function(val, index) {
							if (val.default) {
								that.addressData = val;
							}
						})
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//查询积分商品
			inquiryPointProductById(productId) {
				let that = this;
				let postData = {
					productUuid: productId
				}
				this.$api.request.pointGoodsDetail(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.product = res.body.data;
						if(this.productSkuId){
							this.product.skuList.forEach(function(val,index){
								if(that.productSkuId === val.productSkuUuid){
									that.productSku = val;
								}
							})
						}
						if(!this.product.skuEnabled)
							that.productPoint = (that.product.unitPoint * this.unit);
						else
							that.productPoint = (that.productSku.skuUnitPoint * this.unit);
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			toActualAmount(productAmount, deductAmount, freightAmount){
				return productAmount - deductAmount + freightAmount;
			},
			// 计算运费金额
			calculatePointFreightAmount(province, city, area) {
				let postData = {
					productDTO: {
						productUuid: this.productId
					},
					productSkuDTO: {
						productSkuUuid: this.productSkuId
					},
					orderDTO: {
						productUnit: this.unit,
						deliveryProvince: province,
						deliveryCity: city,
						deliveryArea: area
					}
					
				}
				this.$api.request.calculatePointFreightAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.freightAmount = res.body.data.orderDTO.freightAmount;
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			change(e) {
				console.log('是否打开:' + e.show)
			},
			togglePopup(type, open) {
				switch (type) {
					case 'top':
						this.content = '顶部弹出 popup'
						break

					case 'bottom':
						this.content = '底部弹出 popup'
						break
					case 'center':
						this.content = '居中弹出 popup'
						break
				}
				this.type = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			submit() {
				//检查收货地址是否选择
				if (!this.addressData.userDeliveryAddressUuid) {
					this.$api.msg('请选择收货地址');
					return;
				}
				//post订单数据到后台
				let postData = {
					productDTO: {
						productUuid: this.productId
					},
					productSkuDTO: {
						productSkuUuid: this.productSkuId
					},
					orderDTO: {
						userDTO: {
							userUuid: this.userInfo.userUuid
						},
						productUnit: this.unit,
						deliveryAddressDTO: {
							userDeliveryAddressUuid: this.addressData.userDeliveryAddressUuid
						},
						deliveryType: '1'
					}
				}
				this.$api.request.createPointOrder(postData, res => {
					if (res.body.status.statusCode === '0') {
						//如果需要支付余额
						if(this.actualAmount>0){
							uni.redirectTo({
								url: '/pages/point/point-pay?orderNo=' + res.body.data.orderNo
							})
						}
						//如果不需要支付余额
						else{
							uni.redirectTo({
								url: '/pages/point/point-pay-success'
							})
						}
						
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});

			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}
	.no-merchant{
		.address{
			text-align: center;
			padding: 40upx;
			color: $font-color-dark;
			font-size: $font-lg+4upx;
		}
		.desc{
			text-align: center;
			margin: 40upx;
			color: $font-color-base;
		}
	}
	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		margin: 20upx 0;
		background: #fff;
		position: relative;
		.left{
			width: 160upx;
			padding-right: 10upx;
		}
		.middle{
			
		}
		.action {
			width: 200upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: right;
			color: $font-color-light;
			font-size: $font-base;
			text-align: right;
			.button-base-outline{
				width: 160upx;
			}
		}
		.no-merchant{
			margin-right: auto;
			font-size: $font-lg;
			color: $font-color-dark;
		}
	}
	.address-box {
		display: flex;
		align-items: center;
	
		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
	
		.merchant {
			font-size: $font-lg;
			color: $font-color-dark;
		}
	}
	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
	
		.name {
			margin-right: 30upx;
		}
	}
	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 160upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
				padding-top: 10upx;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: $page-color-light;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: $uni-color-primary;
			border-radius: 4upx;
			margin-right: 12upx;

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-light;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}

			& .close {
				color: $font-color-light;
				margin-left: 20upx;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	.footer {
		position: fixed;
		bottom: var(--window-bottom);
		display: flex;
		align-items: center;
		width: 100%;
		height: 140upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);
		padding: 0 30upx;
		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			align-items: center;
			justify-content: center;
			width: 280upx;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: var(--window-bottom);
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: $page-color-light;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}
</style>
